package com.igeek.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.igeek.boot.entity.ZhrjAlarmInfo;
import com.igeek.boot.mapper.ZhrjAlarmInfoMapper;
import com.igeek.boot.result.DeptIdDTO;
import com.igeek.boot.result.EventDateDTO;
import com.igeek.boot.result.EventTypeDTO;
import com.igeek.boot.service.IZhrjAlarmInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 事件表 服务实现类
 * </p>
 *
 * @author fengqq
 * @since 2024-08-23
 */
@Service
public class ZhrjAlarmInfoServiceImpl extends ServiceImpl<ZhrjAlarmInfoMapper, ZhrjAlarmInfo> implements IZhrjAlarmInfoService {

    @Autowired
    private ZhrjAlarmInfoMapper alarmInfoMapper;

    //事件分类查询
    @Override
    public Map<String, Object> eventStatistic() {
        //1.查询结果包括不同类型事件数量，每种类型及数量在一个对象中  List<EventTypeDTO>
        QueryWrapper<ZhrjAlarmInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("event_type","count(*) as count").groupBy("event_type");
        List<Map<String, Object>> maps = this.baseMapper.selectMaps(queryWrapper);
        //List<Map<String, Object>> --> List<EventTypeDTO>
        List<EventTypeDTO> list1 = maps.stream().map(map -> {
            Integer eventType = Integer.parseInt(map.get("event_type").toString());
            Long count = Long.valueOf(map.get("count").toString());
            return new EventTypeDTO(eventType, count);
        }).collect(Collectors.toList());


        //2.查询结果包括不同（维护）部门的事件数量，每个部门及数量在同一个对象中  List<DeptIdDTO>
        QueryWrapper<ZhrjAlarmInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("dept_id","count(*) as count").groupBy("dept_id");
        List<Map<String, Object>> maps2 = baseMapper.selectMaps(queryWrapper2);
        List<DeptIdDTO> list2 = maps2.stream().map(map -> {
            return new DeptIdDTO(
                    Integer.parseInt(map.get("dept_id").toString()),
                    Long.valueOf(map.get("count").toString())
            );
        }).collect(Collectors.toList());

        //3.返回结果中同时包括(1)、(2)
        HashMap<String, Object> map = new HashMap<>();
        map.put("eventTypeList",list1);
        map.put("deptIdList",list2);
        return map;
    }

    //月度事件数量趋势
    @Override
    public List<EventDateDTO> eventMonthCount(int year, int month) {
        QueryWrapper<ZhrjAlarmInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("date(event_time) as event_date","count(*) as count")
                        .eq("year(event_time)",year)
                        .eq(" month(event_time)",month)
                        .groupBy("date(event_time)");
        List<Map<String, Object>> maps = baseMapper.selectMaps(queryWrapper);
        List<EventDateDTO> list = maps.stream().map(map -> {
            //String  2023-03-13 ==> LocalDate
            LocalDate eventDate =
                    LocalDate.parse(map.get("event_date").toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            Long count = Long.valueOf(map.get("count").toString());
            return new EventDateDTO(eventDate, count);
        }).collect(Collectors.toList());
        return list;
    }

    //多表查询
    @Override
    public List<ZhrjAlarmInfo> findAllAlarmInfo(String wanIp, Long deptId, Integer eventType) {
        List<ZhrjAlarmInfo> list = alarmInfoMapper.findAllAlarmInfo(wanIp,deptId,eventType);
        return list;
    }
}
